package com.mvity.framework.common.web.controller;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.mvity.framework.common.annotation.ControllerDesc;
import com.mvity.framework.common.base.BaseController;
import com.mvity.framework.common.cache.SessionCache;
import com.mvity.framework.common.global.ResultCode;
import com.mvity.framework.system.cache.UserAccountCache;
import com.mvity.framework.system.cache.UserCache;
import com.mvity.framework.system.service.UserService;

/**
 * 系统控制器
 * 
 * @author vity
 * @date 2012-7-22
 * @mail vity.1215@gmail.com
 */
@Controller
public final class AppController extends BaseController {

	private static final long serialVersionUID = -5308736516967557835L;

	@Resource
	private UserService userService;

	@ControllerDesc("打开首页")
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index(@CookieValue(value = "JSESSIONID", required = false) String sessionId) {
		if (StringUtils.isEmpty((String) SessionCache.getCache().get(sessionId))) {
			return "forward:login";
		} else {
			return "forward:app";
		}
	}

	/**
	 * 跳转到主页
	 */
	@ControllerDesc("跳转到主页")
	@RequestMapping(value = "/app")
	public String app() {
		return "app";
	}

	/**
	 * 加载首页内容
	 */
	@ControllerDesc("加载首页")
	@RequestMapping(value = "/appContent")
	public String appContent() {
		return "appContent";
	}

	/**
	 * init data
	 */
	@ControllerDesc("Init... Datas.")
	@RequestMapping(value = "/init")
	public String init() {
		// userService.initData();
		return "forward:/";
	}

	/**
	 * 跳转到登陆页面
	 */
	@ControllerDesc("跳转到登录页")
	@RequestMapping(value = "/login")
	public String loginPage() {

		return "login";
	}

	/**
	 * 验证登陆
	 * 
	 * @return
	 */
	@ControllerDesc("验证登陆")
	@RequestMapping(value = "/userLogin", method = RequestMethod.POST)
	public String userLogin(@CookieValue(value = "JSESSIONID", required = false) String sessionId, String userName, String password) {
		String result = userService.userLogin(userName, password);
		switch (result) {
		case ResultCode.NotFoundAccount:
		case ResultCode.AccountPasswordInvalid:
			logger.info(userName + " 登陆失败 " + result);
			return "forward:login";
		}
		try {
	        userService.testAspect(null, 0, 'x', null,null,null, false, null);
        } catch (Exception e) {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
        }
		logger.info(userName + " 登陆成功");
		SessionCache.getCache().writeCache(sessionId, result);
		return "forward:app";
	}

	/**
	 * 处理 404
	 */
	@ControllerDesc("跳转到404页面")
	@RequestMapping(value = "/error/404")
	public String handler404() {
		return "error/404";
	}

	/**
	 * 处理 500
	 */
	@ControllerDesc("跳转到异常页面")
	@RequestMapping(value = "/error/500")
	public String handler500() {
		return "error/500";
	}
}
